<template>
  <div class="auth-page">
    <div class="decorations">
      <div class="gradient-circle orange"></div>
      <div class="gradient-circle blue"></div>
    </div>

    <div class="auth-container">
      <div class="logo-section">
        <h1 class="logo">技术问答与在线互助论坛系统</h1>
        <p class="slogan">技术问答与在线互助论坛系统</p>
      </div>

      <div class="form-section">
        <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入账号" clearable>
              <template #prefix>
                <img :src="emailIconUrl" style="width: 16px; height: 16px; margin-right: 8px;" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password>
              <template #prefix>
                <img :src="passwordIconUrl" style="width: 16px; height: 16px; margin-right: 8px;" />
              </template>
            </el-input>
          </el-form-item>

          <div class="flex justify-between items-center mb-6">
            <el-checkbox v-model="rememberMe">记住我</el-checkbox>
          </div>

          <el-button type="primary" class="submit-btn" @click="handleLogin" :loading="loading">
            登录
          </el-button>

          <div class="text-center mt-6 text-sm text-gray-500">
            <div class="flex justify-end">
              <el-link type="primary" :underline="false" @click="goToRegister">立即注册</el-link>
            </div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { setToken } from '@/utils/cookie'
import { loginService } from '@/apis/user'

// 使用 Vite 的静态资源导入语法
import emailIcon from '../assets/images/youxiang.png'
import passwordIcon from '../assets/images/yanzhengma.png'

const router = useRouter()
const rememberMe = ref(false)

// 登录表单
const loginForm = ref({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 4, max: 16, message: '长度在4到16个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在6到20个字符', trigger: 'blur' }
  ]
}

const loading = ref(false)

// 图片 URL
const emailIconUrl = emailIcon
const passwordIconUrl = passwordIcon


// 登录方法
const handleLogin = async () => {
  try {
    loading.value = true
    
    // 调用登录接口
    const loginResult = await loginService(
      loginForm.value.username, 
      loginForm.value.password
    )
    
    ElMessage.success('登录成功')
    router.push('/forum/layout')
    // 设置token到cookie
    setToken(loginResult.data)
  } catch (error) {
    ElMessage.error(error.message || '登录失败')
  } finally {
    loading.value = false
  }
}

// 跳转到注册页
const goToRegister = () => {
  router.push('/forum/register')
}
</script>

<style lang="scss" scoped>
.auth-page {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background: #f8f9fa;
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;

  .decorations {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 0;
    overflow: hidden;

    .gradient-circle {
      position: absolute;
      border-radius: 50%;
      filter: blur(60px);
      opacity: 0.7;
      transition: all 0.8s cubic-bezier(0.4, 0, 0.2, 1);

      &.orange {
        width: 500px;
        height: 500px;
        background: linear-gradient(135deg, #f72585 0%, #f8961e 100%);
        left: 10%;
        top: 30%;
      }

      &.blue {
        width: 400px;
        height: 400px;
        background: linear-gradient(135deg, #4361ee 0%, #4cc9f0 100%);
        top: 15%;
        right: 10%;
      }
    }
  }

  .auth-container {
    width: 100%;
    max-width: 480px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    padding: 48px;
    z-index: 1;
    backdrop-filter: blur(5px);
    transition: all 0.4s;

    &:hover {
      box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
      transform: translateY(-2px);
    }

    .logo-section {
      .logo {
        font-size: 36px;
        font-weight: 700;
        margin-bottom: 12px;
        background: linear-gradient(90deg, #4361ee, #3f37c9);
        background-clip: text;
        -webkit-background-clip: text;
        -moz-background-clip: text;
        color: transparent;
        -webkit-text-fill-color: transparent;
        transition: all 0.4s;

        @supports not (background-clip: text) {
          color: #4361ee;
          background: none;
        }
      }

      .slogan {
        font-size: 16px;
        color: #6c757d;
        letter-spacing: 0.5px;
      }
    }

    .el-form-item {
      margin-bottom: 24px;
    }

    .submit-btn {
      width: 100%;
      height: 48px;
      font-size: 16px;
      font-weight: 500;
      margin-top: 16px;
      background: linear-gradient(90deg, #4361ee, #3f37c9);
      border: none;
      border-radius: 8px;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 5px 15px rgba(67, 97, 238, 0.3);
      }

      &:active {
        transform: translateY(0);
      }
    }

    .el-input {
      transition: all 0.3s;

      &:hover {
        .el-input__wrapper {
          box-shadow: 0 0 0 1px rgba(67, 97, 238, 0.3);
        }
      }

      .el-input__wrapper {
        padding: 0 15px;
        height: 48px;
        border-radius: 8px;
        transition: all 0.3s;

        &.is-focus {
          box-shadow: 0 0 0 1px #4361ee !important;
        }
      }
    }
  }
}

@media (max-width: 600px) {
  .auth-page {
    padding: 20px;

    .auth-container {
      padding: 30px 24px;

      .logo-section {
        margin-bottom: 30px;

        .logo {
          font-size: 28px;
        }
      }
    }
  }
}
</style>    